export default {
    namespaced: true,
    state: {
        token: uni.getStorageSync('user_token'),
        userInfo: uni.getStorageSync('user_info'),
        tokenExpired: uni.getStorageSync('user_token_expired'),
    },
    getters: {
        isTokenValid(state) {
            return !!state.token && state.tokenExpired > Date.now()
        }
    },
    mutations: {
        set_token: (state, {token, userInfo, tokenExpired = 0}) => {
            state.token = token
            state.userInfo = userInfo
            state.tokenExpired = tokenExpired
            uni.setStorageSync('user_token', token)
            uni.setStorageSync('user_info', userInfo)
            uni.setStorageSync('user_token_expired', tokenExpired)
        },
        remove_token: (state) => {
            state.token = ''
            state.userInfo = {}
            state.tokenExpired = 0
            uni.removeStorageSync('user_info')
            uni.removeStorageSync('user_token')
            uni.removeStorageSync('user_token_expired')
        }
    },
    actions: {}
}
